package org.itboys.core.controller.front;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.ErrorException;
import org.itboys.core.common.ExceptionEnum;
import org.itboys.core.service.mongo.front.SundayFrontAddressService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.spring.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Auther: 罗尧坤
 * Date: 2018/8/1
 * Description: 小程序: 收货地址
 */

@Controller
@RequestMapping("/sunday/small/address")
@Api(value = "address", description = "收货地址")
public class SundayFrontAddressController extends BaseController {
    @Autowired
    private SundayFrontAddressService addressService;

    /**
     * 查询用户收货地址
     *
     * @param
     */
    @RequestMapping(value = "/select", method = {RequestMethod.POST})
    @ApiOperation(value = "【用户端】查询用户收货地址。")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "memberId", value = "用户id") @RequestParam(value = "memberId", required = true) Long memberId
    ) {
        try {
            ResponseMessage.success(addressService.select(request, memberId));
        } catch (Exception e) {
            String  msg  = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/address/select----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }

    /**
     * 保存新增或更新用户地址
     *
     * @param request
     * @param response
     * @param id
     * @param receiveName
     * @param receiveMobile
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "【用户端】保存用户地址")
    public void save(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "lable", value = "标签: 家, 或者公司")
            @RequestParam(value = "lable", required = true) String lable
            ,
            @ApiParam(required = true, name = "memberId", value = "用户id")
            @RequestParam(value = "memberId", required = true) Long memberId
            ,
            @ApiParam(required = true, name = "id", value = "地址id。新增时传入0。")
            @RequestParam(value = "id", required = true) Long id
            ,
            @ApiParam(required = true, name = "receiveName", value = "收货人姓名。")
            @RequestParam(value = "receiveName", required = true) String receiveName
            ,
            @ApiParam(required = true, name = "receiveMobile", value = "手机号码。")
            @RequestParam(value = "receiveMobile", required = true) String receiveMobile
            ,
            @ApiParam(required = false, name = "provinceId", value = "省ID。")
            @RequestParam(value = "provinceId", required = false) Long provinceId
            ,
            @ApiParam(required = false, name = "provinceName", value = "省名称。")
            @RequestParam(value = "provinceName", required = false) String provinceName
            ,
            @ApiParam(required = false, name = "cityId", value = "市ID。")
            @RequestParam(value = "cityId", required = false) Long cityId
            ,
            @ApiParam(required = false, name = "cityName", value = "市名称。")
            @RequestParam(value = "cityName", required = false) String cityName
            ,
            @ApiParam(required = false, name = "districtId", value = "区县ID。")
            @RequestParam(value = "districtId", required = false) Long districtId
            ,
            @ApiParam(required = false, name = "districtName", value = "区县名称。")
            @RequestParam(value = "districtName", required = false) String districtName
            ,
            @ApiParam(required = true, name = "streetId", value = "街道id。")
            @RequestParam(value = "streetId", required = true) Long streetId
            ,
            @ApiParam(required = true, name = "streetName", value = "街道名称。")
            @RequestParam(value = "streetName", required = true) String streetName
            ,
            @ApiParam(required = true, name = "roadName", value = "道路名称。")
            @RequestParam(value = "roadName", required = true) String roadName
            ,
            @ApiParam(required = true, name = "villageId", value = "小区id。")
            @RequestParam(value = "villageId", required = true) Long villageId
            ,
            @ApiParam(required = true, name = "villageName", value = "小区名称。")
            @RequestParam(value = "villageName", required = true) String villageName
            ,
            @ApiParam(required = true, name = "detailedAddress", value = "详细地址。")
            @RequestParam(value = "detailedAddress", required = true) String detailedAddress
            ,
            @ApiParam(required = true, name = "isDefault", value = "是否默认。")
            @RequestParam(value = "isDefault", required = true) Integer isDefault
    ) {
        try {
            addressService.save(
                    id,
                    lable,
                    memberId,
                    receiveName,
                    receiveMobile,
                    provinceId,
                    provinceName,
                    cityId,
                    cityName,
                    districtId,
                    districtName,
                    streetId,
                    streetName,
                    roadName,
                    villageId,
                    villageName,
                    detailedAddress,
                    isDefault);
            ResponseMessage.success();
        } catch (Exception e) {
            String  msg  = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/address/save----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }

    /**
     * 单个删除收货地址
     *
     * @param request
     * @param response
     * @param addressId
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "【用户端】删除地址")
    public void delete(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "addressId", value = "地址id。") @RequestParam(value = "addressId", required = true) Long addressId
    ) {
        try {
            addressService.delete(addressId);
            ResponseMessage.success();
        } catch (Exception e) {
            String  msg  = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;

            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/address/delete----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }

    @ApiOperation(value = "【用户端】查找一个详细收货地址")
    @RequestMapping(value = "/fineOne", method = RequestMethod.POST)
    public void fineOne(HttpServletRequest request,
                        HttpServletResponse response,
                        @ApiParam(value = "id") @RequestParam(value = "id", required = true) Long id
    ) throws Exception {

        ResponseMessage.success(addressService.findOne(id));

    }


}
